package com.unina.dropplayer.play;

import android.os.AsyncTask;
import android.widget.ImageButton;
import android.widget.TextView;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.exception.DropboxException;
import com.example.dropplayer.R;

public class PlayTask extends AsyncTask<String, String, String> {

	private ImageButton btnPlay;
	private TextView songTitleLabel;
	private DropboxAPI<?> mDBApi;
	private String songTitle;

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub

		try {
			songTitle = params[0];
			if(null!=songTitle && -1 != songTitle.lastIndexOf('/')){
				songTitle = songTitle.substring(songTitle.lastIndexOf('/')+1, songTitle.length());
			}
			return mDBApi.media(params[0], false).url;
		} catch (DropboxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public PlayTask(ImageButton btnPlay
			, TextView songTitleLabel,
			DropboxAPI<?> mDBApi) {
		super();
		this.btnPlay = btnPlay;
		this.songTitleLabel = songTitleLabel;
		this.mDBApi = mDBApi;
	}

	@Override
	protected void onPostExecute(String result) {

		super.onPostExecute(result);

		songTitleLabel.setText(songTitle);

		// Changing Button Image to pause image
		btnPlay.setImageResource(R.drawable.btn_pause);

	}

}
